<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物品编码" prop="goods_code">
        <el-input
          v-model="queryParams.goods_code"
          placeholder="请输入物品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batch_code">
        <el-input
          v-model="queryParams.batch_code"
          placeholder="请输入入库批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="warehouse_id">
        <el-select v-model="queryParams.warehouse_id" clearable placeholder="请选择仓库">
          <el-option
            v-for="item in receiveName"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="库位" prop="area_id">
        <el-select v-model="queryParams.area_id" clearable placeholder="请选择库位">
          <el-option
            v-for="item in areaNames"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="库存有效期">
        <el-date-picker
          v-model="daterangeExpire_date"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="stockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事务" align="center" prop="id" />
      <el-table-column label="物品编码" align="center" prop="goods_code" />
      <el-table-column label="单位" align="center" prop="measure_unit" />
      <el-table-column label="入库批次号" align="center" prop="batch_code" />
      <el-table-column label="仓库" align="center" prop="warehouse_id" />
      <el-table-column label="库位" align="center" prop="area_id" />
      <el-table-column label="供应商" align="center" prop="supplier_id" />
      <el-table-column label="生产厂商" align="center" prop="manufacturer_id" />
      <el-table-column label="在库数量" align="center" prop="quantity" />
      <el-table-column label="库存有效期" align="center" prop="expire_date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expire_date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="create_by" />
      <el-table-column label="入库时间" align="center" prop="create_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.create_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="update_by" />
      <el-table-column label="更新时间" align="center" prop="update_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.update_time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存数量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物品编码" prop="goods_code">
          <el-input v-model="form.goods_code" placeholder="请输入物品编码" />
        </el-form-item>
        <el-form-item label="单位" prop="measure_unit">
          <el-input v-model="form.measure_unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="入库批次号" prop="batch_code">
          <el-input v-model="form.batch_code" placeholder="请输入入库批次号" />
        </el-form-item>
        <el-form-item label="仓库" prop="warehouse_id">
          <el-select v-model="form.warehouse_id" clearable placeholder="请选择收货仓库">
            <el-option
              v-for="item in listName"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库位" prop="area_id">
          <el-select v-model="form.area_id" clearable placeholder="请选择">
            <el-option
              v-for="item in areaNames"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplier_id">
          <el-input v-model="form.supplier_id" placeholder="请输入供应商ID" />
        </el-form-item>
        <el-form-item label="生产厂商" prop="manufacturer_id">
          <el-input v-model="form.manufacturer_id" placeholder="请输入生产厂商id" />
        </el-form-item>
        <el-form-item label="在库数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入在库数量" />
        </el-form-item>
        <el-form-item label="库存有效期" prop="expire_date">
          <el-date-picker clearable
            v-model="form.expire_date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择库存有效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStock, getStock, delStock, addStock, updateStock } from "@/api/wms/stock";
import {getAreaWarehouse, getOrderListName, getOrderListReceiveName} from "@/api/wms/order";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      receiveName: {},
      areaNames: {},
      listName: {},
      // 库存数量表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeExpire_date: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goods_code: null,
        batch_code: null,
        warehouse_id: null,
        area_id: null,
        expire_date: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getOrderListReceiveName();
    this.getAreaWarehouse()
    this.getOrderListName();
  },
  methods: {
    getOrderListName() {
      getOrderListName().then(res => {
        this.listName = res.data
      })
    },
    getAreaWarehouse() {
      getAreaWarehouse().then(res => {
        this.areaNames = res.data
      })
    },
    getOrderListReceiveName() {
      getOrderListReceiveName().then(res => {
        this.receiveName = res.data
      })
    },
    /** 查询库存数量列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangeExpire_date && '' != this.daterangeExpire_date) {
        this.queryParams.beginExpire_date = this.daterangeExpire_date[0];
        this.queryParams.endExpire_date = this.daterangeExpire_date[1];
      }
      listStock(this.queryParams).then(response => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goods_code: null,
        measure_unit: null,
        batch_code: null,
        warehouse_id: null,
        area_id: null,
        supplier_id: null,
        manufacturer_id: null,
        quantity: null,
        expire_date: null,
        remark: null,
        create_by: null,
        create_time: null,
        update_by: null,
        update_time: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeExpire_date = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存数量";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStock(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存数量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStock(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库存数量编号为"' + ids + '"的数据项？').then(function() {
        return delStock(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/stock/export', {
        ...this.queryParams
      }, `stock_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
